part of shop;

class UserPage extends StatefulWidget {
  const UserPage({Key? key}) : super(key: key);
  @override
  State<StatefulWidget> createState() {
    return UserPageState();
  }
}

class UserPageState extends State<UserPage> with TickerProviderStateMixin {

  String name = '未登录';
  String id = '----';

  final User _user=User();

  static const String defaultBackground =
      'https://cdn.nlark.com/yuque/0/2022/jpeg/23159950/1664536772010-7babba34-b1ce-4551-bc9a-0478261af05f.jpeg';
  static const String defaultAvatar =
      'https://cdn.nlark.com/yuque/0/2022/jpeg/23159950/1664536816290-ddf2ce0b-d9fe-4c76-a729-29ccbafbc242.jpeg';

  /// 这个用来控制appbar透明度
  // late ValueNotifier<double> _notifier;

  /// 监听列表滚动位置，从而更改appbar透明度
  late ScrollController _scrollController;
  // late TabController _tabController;
  @override
  void initState() {
    super.initState();
    if(_user.statusCode==0){
      name=_user.info.name;
      id=_user.info.id;
    }else if (_user.statusCode==-1){
      _user.getLocalState().then((loginState){
        if(loginState==0){
          name=_user.info.name;
          id=_user.info.id;
        }
      });
    }
    // _notifier = ValueNotifier(0.0);
    _scrollController = ScrollController();
    // _tabController = TabController(length: 3, vsync: this);
    // _scrollController.addListener(() {
    //   _notifier.value =
    //       _scrollController.offset > 100 ? 1 : _scrollController.offset / 120;
    // });
  }

  @override
  Widget build(BuildContext context) {

    ///-1:有token，无info，出错
    ///0:有token，有info，已登录
    ///1：无token，有info，未登录
    ///2：无token，无info，未注册
    // if (myUser.statusCode == 0) {
    //   name = myUser.info.name;
    //   id = myUser.info.id;
    //   avatarUrl = myUser.info.avatar;
    //   linkedPage = Scaffold(
    //     body: Column(
    //       children: [
    //         Text("token:${myUser.token}"),
    //         const Divider(),
    //         Text(
    //           "info:${myUser.info}",
    //           maxLines: 4,
    //         ),
    //       ],
    //     ),
    //   );
    // }

    // var backGround = Image.network(
    //   AppConfig.tempImg,
    //   fit: BoxFit.fitWidth,
    // );
    return Scaffold(
      backgroundColor: const Color.fromRGBO(238, 238, 238, 1.0),
      extendBody: true,
      extendBodyBehindAppBar: true,
      appBar: null,
      body: ListView(
        // physics: const BouncingScrollPhysics(),
        physics: const AlwaysScrollableScrollPhysics(),
        padding: const EdgeInsets.only(top: 0),
        controller: _scrollController,
        children: [
          SizedBox(
            // height: MediaQuery.of(context).size.width * 3 / 4-100,
            // alignment: Alignment.center,
            child: Stack(
              fit: StackFit.loose,
              children: [
                SizedBox(
                  child: Column(
                    children: [
                      AspectRatio(
                          //背景图比例
                          aspectRatio: 2.0,
                          // TODO 动态加载用户背景图
                          child: FadeInImage(
                                //这是用户页背景图
                                image: NetworkImage(defaultBackground),
                                imageErrorBuilder: (context,object,trace){
                                  return Image.asset("images/defaultBackground.bmp",
                                    width: MediaQuery.of(context).size.width,
                                    fit: BoxFit.cover,
                                  );
                                },
                                placeholder:const AssetImage("images/defaultBackground.bmp"),
                                width: MediaQuery.of(context).size.width,
                                //height: MediaQuery.of(context).size.width*5/8,
                                fit: BoxFit.cover,
                              ),
                      ),
                      Container(
                        color: Colors.white,
                        alignment: Alignment.center,
                        height: MediaQuery.of(context).size.width / 6,
                        padding: EdgeInsets.only(
                             left: MediaQuery.of(context).size.width / 3,

                        ),
                        child: ListTile(
                            title: Text(
                              name,
                              style: const TextStyle(
                                fontSize: 20,
                                fontWeight: FontWeight.bold,
                              ),
                            ),
                            subtitle: Text(
                              "ID $id",
                              style: const TextStyle(
                                color: Colors.grey,
                                fontSize: 12,
                              ),
                            ),
                          onTap: (){
                              if(_user.statusCode==0){
                                Navigator.of(context).push(
                                    MaterialPageRoute(builder: (context)=>
                                        Scaffold(
                                          appBar: AppBar(title:Text("用户信息页(待完善)")),
                                          body: Center(child:Text(_user.info.toString()),),
                                        )
                                    ));
                              }else{
                                Navigator.push(context, MaterialPageRoute(builder: (context)=>LoginPage()));
                              }
                          },
                        ),
                      )
                    ],
                  ),
                ),
                Positioned(
                  bottom: 0,
                  //top: MediaQuery.of(context).size.width * 5 / 12,
                  left: 20,
                  child: CircleAvatar(
                    backgroundColor: Colors.white,
                    radius: MediaQuery.of(context).size.width / 8,
                    child: CircleAvatar(
                      radius: MediaQuery.of(context).size.width / 8 - 5,
                      // TODO 图片缓存
                      backgroundImage: _user.statusCode==0?
                        NetworkImage(_user.info.avatar): const NetworkImage(defaultAvatar),
                    ),
                  ),
                )
              ],
            ),
          ),
          Card(
            margin: const EdgeInsets.all(8),
            color: Colors.white,
            elevation: 2,

            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              children: [
                InkWell(
                  onTap: ()  {
                    Navigator.push(context,
                        MaterialPageRoute(builder: (context) {
                          return PostGoodsPage();
                        }));
                    // Navigator.push(context,
                    //     MaterialPageRoute(builder: (context) {
                    //   return LikedPage();
                    // }));
                  },
                  borderRadius: BorderRadius.circular(10),
                  child: const ListTile(
                    leading: Icon(
                      Icons.upload_outlined,
                      color: Colors.purpleAccent,
                    ),
                    title: Text('发布商品'),
                  ),
                ),
                const Divider(
                  height: 1,
                  thickness: 1,
                  indent: 20,
                  endIndent: 20,
                ),
                InkWell(
                  onTap: () {},
                  borderRadius: BorderRadius.circular(10),
                  child: const ListTile(
                    leading: Icon(
                      Icons.monetization_on_outlined,
                      color: Colors.blue,
                    ),
                    title: Text('发布的商品'),
                  ),
                ),
                const Divider(
                  height: 1,
                  thickness: 1,
                  indent: 20,
                  endIndent: 20,
                ),
                InkWell(
                  onTap: () {},
                  borderRadius: BorderRadius.circular(10),
                  child: const ListTile(
                    leading: Icon(
                      Icons.shop,
                      color: Colors.orangeAccent,
                    ),
                    title: Text('我发布的商品'),
                  ),
                ),
              ],
            ),
          ),

          Card(
            margin: const EdgeInsets.all(8),
            color: Colors.white,
            elevation: 2,
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(10),
            ),
            child: Column(
              children: [
                InkWell(
                  onTap: () {},
                  borderRadius: BorderRadius.circular(10),
                  child: const ListTile(
                    leading: Icon(
                      Icons.access_time,
                      color: Colors.redAccent,
                    ),
                    title: Text('浏览历史'),
                  ),
                ),
                const Divider(
                  height: 1,
                  thickness: 1,
                  indent: 20,
                  endIndent: 20,
                ),
                InkWell(
                  onTap: () {

                  },
                  borderRadius: BorderRadius.circular(10),
                  child: const ListTile(
                    leading: Icon(
                      Icons.settings_outlined,
                      color: Colors.green,
                    ),
                    title: Text('设置'),
                  ),
                ),
                const Divider(
                  height: 1,
                  thickness: 1,
                  indent: 20,
                  endIndent: 20,
                ),
                InkWell(
                  onTap: () {},
                  borderRadius: BorderRadius.circular(10),
                  child: const ListTile(
                    leading: Icon(Icons.info_outline, color: Colors.cyan),
                    title: Text('关于'),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

// final ButtonStyle _buttonStyle = ButtonStyle(
//     shape: MaterialStateProperty.all<RoundedRectangleBorder>(
//       RoundedRectangleBorder(
//         borderRadius: BorderRadius.circular(45.0),
//       ),
//     ),
//     elevation: MaterialStateProperty.all(0),
//     shadowColor: MaterialStateProperty.all(Colors.transparent),
//     backgroundColor: MaterialStateProperty.all(Colors.transparent));
